package kr.couponplay.android.activity.message;

import java.util.List;

import kr.couponplay.android.activity.R;
import kr.couponplay.android.domain.message.MessageList;
import android.app.AlertDialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class MessageListFragment extends Fragment implements OnItemClickListener {
	private MessageActivity activity;
	protected MessageListViewAdapter messageListViewAdapter;
	protected ListView messageListView;
	private int position;
	
	private List<MessageList.Item> messageItems;
	
	public static MessageListFragment newInstance(MessageActivity activity, int position, List<MessageList.Item> messageItems) {
		MessageListFragment fragment = new MessageListFragment();
		fragment.activity = activity;
		fragment.position = position;
		fragment.messageItems = messageItems;
		return fragment;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View onCreateView = inflater.inflate(R.layout.message_fragment, container, false);

        messageListViewAdapter = new MessageListViewAdapter(container.getContext(), R.layout.message_list_row, messageItems);
        messageListView = (ListView) onCreateView.findViewById(R.id.message_list_view);
        messageListView.setAdapter(messageListViewAdapter);	
        messageListView.setOnItemClickListener(this);
		return onCreateView;
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
		String message = "[" + messageListViewAdapter.getItem(position).getMessage() + "]<br/>";
        new AlertDialog.Builder(activity)
            .setMessage(Html.fromHtml(message))
            .setNegativeButton("확인", null)
            .show();
	}

	public int getPosition() {
		return position;
	}
}
